package Back_end;

import java.util.*;

public class BookManager {
    private LinkedList<Book> box = new LinkedList<>();
    private HashMap<String,Integer> bookHashMap = new HashMap<String, Integer>();
    private int num = 0;
    public BookManager(){
        SQL.Reading(this);
    }
    public LinkedList<Book> search(String s){
        LinkedList<Book> results = new LinkedList<>();
        int num = 0;
        for (Book book : box) {
            if (book.getName().contains(s)||book.getAuthor().contains(s)){
                results.add(book);
                Connect conn = new Connect();
                ++num;
            }
            if(num==0)
            {
                System.out.println("找不到你所搜索的书籍");
            }
        }
        if(results.size()!=0)
        for (Book book:results) {
            System.out.println(book.toString());
        }
        else
            System.out.println("找不到你所搜索的书");
        return results;
    }
    public void add(Book b) {
        if (this.bookHashMap.containsKey(b.getLoginID()))
            System.out.println("Error!!!\n此书的LoginID已被注册");
        else {
            this.box.add(b);
            bookHashMap.put(b.getLoginID(),this.num);
            this.num++;
            SQL.Add(b);
        }
    }
    public void SQLAdd(Book b){
        this.box.add(b);
        bookHashMap.put(b.getLoginID(),this.num);
        this.num++;
    }
    //书的loginID作为删除和修改的主键
    public LinkedList<Book> show(){
        ListIterator<Book> l = this.box.listIterator();
        //System.out.println("LoginID\t\tName\t\tAuthor\t\tClassCode\t\tPublicationUnit\t\tPublicationTime\t\tPrice\t\t");
        while(l.hasNext()){
            System.out.println(l.next().toString());
        }
        return this.box;
    }
    public void delete(String delete){
        this.show();
        Scanner input  =new Scanner(System.in);
        String in = input.nextLine();
        if (bookHashMap.containsKey(in)){
            box.remove(bookHashMap.get(in));
            bookHashMap.remove(in);
            SQL.Delete(in);
        }
        else {
            System.out.println("请输入正确的LoginID");
        }
    }
    public boolean judge(String LoginID){
        if (bookHashMap.containsKey(LoginID))
            return true;
        return false;
    }
    public boolean update(Book book){
     this.show();
     Scanner input = new Scanner(System.in);
        if (bookHashMap.containsKey(book.getLoginID())){
            box.get(bookHashMap.get(book.getLoginID())).setName(book.getName());
            box.get(bookHashMap.get(book.getLoginID())).setAuthor(book.getAuthor());
            box.get(bookHashMap.get(book.getLoginID())).setClassCode(book.getClassCode());
            box.get(bookHashMap.get(book.getLoginID())).setPublicationUnit(book.getPublicationUnit());
            box.get(bookHashMap.get(book.getLoginID())).setPublicationTime(book.getPublicationTime());
            box.get(bookHashMap.get(book.getLoginID())).setPrice(book.getPrice());
            SQL.update(box.get(bookHashMap.get(book.getLoginID())));
            return true;
        }
            return false    ;
    }
}
